<?php
require_once __DIR__ . "/db_orm.php";
use Illuminate\Database\Capsule\Manager as DB;

function curl_get_baidu($url)
{
// 构造包头，模拟浏览器请求
    $header = array(
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Encoding: gzip, deflate',
        'Accept-Language: zh-CN,zh;q=0.9',
        'Cache-Control: max-age=0',
        'Connection: keep-alive',
//Cookie: BIDUPSID=B5D68E3304B3AF19A10A67803E5D8811; PSTM=1594266197; BAIDUID=9E98C063AAC1E1CBE3CFC9899EC7C7D3:SL=0:NR=10:FG=1; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; H_WISE_SIDS=148077_150112_151472_146875_149356_150077_147088_141744_150087_148867_151311_148713_150745_147280_150647_150165_149587_151363_151017_147890_148524_127969_151330_151296_146548_148616_149718_146653_150346_146732_150965_138425_151116_149214_131423_146500_128700_147588_147912_107314_148185_149251_150812_140368_151266_144966_146785_150340_148424_147547_146053_148868_110085; plus_cv=1::m:49a3f4a6; BDICON=10294984.98; MSA_WH=414_736; BDUSS=ZDNXA1RE9UWjVPZklnb2dUQ3d5U3F4VUVheXMwdmNuYVVHRmIxNUloUE80RFJmRUFBQUFBJCQAAAAAAAAAAAEAAAB15olM0KHA9MC0wcsAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM5TDV~OUw1fVn; BDSFRCVID=9g0sJeCCxG3_WP5rBJq0XdyMSfIRY2CFAFHL3J; H_BDCLCKID_SF=tJ4toCPMJI_3fP36q4Rh-tFO-frQaC62aKDshtQg-hcqEpO9QTbr3pO0K45J2M7U52DDKl6YLfIKEpQqQx7dDUThDG0tJ6kjJnusL-35HJoHjJbGq4bohjnye-j9BtQmQnTxoM76LI5KElcJ34nVMR_VD4RiWjc9QNnTBJOmLD5aOCtmLROBLpKNypbK0x-jLTn90h6k0qbDOCoE04nJyUnQbPnnBT5i3H8HL4nv2JcJbM5m3x6qLTKkQN3T-PKO5bRu_CF-tIKabKtmD6Rb5nbHMpoKa43K2CJ0sJOOaCvVe4bRy4oTLnk1DnnUQTQH0eQm-t-yQ45IelT-Wj5TQx_zy-keBjIHtnIDVIt2tDvbfP0k-4QEbbQH-UnLqhQp22OZ0lOmJl05_U3MjJ5IylknhUcBb4DHW23wVl7mQUJ8HUb-jJQkD50Y5hrmLf6i-K64KKJxbp7J8qrtQ-5kDxuwhUJiB5OMBan7_qvIXKohJh7FM4tW3J0ZyxomtfQxtNRJ0DnjtpChbC_xjjLWD53XepJf-K6WbC600njVanI_Hn7zep6VQRtpbtbmhU-eXavT_4oPWfn1HfoxhM6WyqD4D4bl3x57Bn7ZVD_MJCDWhK-4enJb5ICV-frb-C62aKDs0KjpBhcqEU-GQTbr3pO0K45Zb6OyaCQD3DocWhuB8Ub20-6j3hj0KtCjtUjJMm7p2qvbKl5nhMJmXPJYXxDv-4CHWljy523ion3vQpP-OpQ3DRoWXPIqbN7P-p5Z5mAqKl0MLPbtbb0xXj_0D6O0DHAjtTDsb5vfsJvaKRTfHJT1MDTjhPrMeftjbMT-027OKKO7QMthqlo_Kj3ibbFI0UjAQqFH02cCKqn1thF0HPonHjDaD6533J; H_PS_PSSID=; BD_UPN=12314753; delPer=0; BD_CK_SAM=1; COOKIE_SESSION=6023_0_2_2_17_1_1_0_2_1_2_0_1460_0_0_0_1594696335_0_1594714785%7C9%2364448_6_1594263531%7C4; BDRCVFR[Wike4cYOxTR]=mk3SLVN4HKm; BDRCVFR[VjobkFsAYtR]=mk3SLVN4HKm; PSINO=6; BDSVRTM=180
        'Host: www.baidu.com',
        'Upgrade-Insecure-Requests: 1',
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36',
    );

    $content = getUrl($url, $header);
    return $content;
}

//神马搜索
function get_sm_data($url)
{
// 构造包头，模拟浏览器请求
    $header = array(
        'Accept' => '*/*',
        'Accept-Encoding' => 'gzip, deflate, br',
        'Accept-Language' => 'zh-CN,zh;q=0.9',
        'Access-Control-Request-Headers' => 'x-hhytrace-flag',
        'Access-Control-Request-Method' => 'POST',
        'Connection' => 'keep-alive',
//        'Host' => 'www.so.com',
        'Sec-Fetch-Dest' => 'empty',
        'Sec-Fetch-Mode' => 'cors',
        'Sec-Fetch-Site' => 'same-origin',
        'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36',
        'content-type' => 'application/x-www-form-urlencoded;charset=UTF-8',
    );
    $content = getUrl($url, $header);
    return $content;
}

/**\
 * @param $html //html文本
 * @param $tag //html标签
 * @param $class //属性
 * @param $value //属性值
 * @return mixed
 */
function get_tag_data($html, $tag, $class, $value)
{
    //$value 为空，则获取class=$class的所有内容
    $regex = $value ? "/<$tag.*?$class=\"$value\".*?>(.*?)<\/$tag>/is" : "/<$tag.*?$class=\".*?$value.*?\".*?>(.*?)<\/$tag>/is";
    preg_match_all($regex, $html, $matches, PREG_PATTERN_ORDER);
    return $matches[1];//返回值为数组 ,查找到的标签内的内容
}

function get_sogou_data($url)
{
    $header = array(
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Encoding: gzip, deflate, br',
        'Accept-Language: zh-CN,zh;q=0.9',
        'Connection: keep-alive',
//        'Cookie: ssuid=9965539240; sw_uuid=79197392; IPLOC=CN4201; SUID=A669111B2B12960A000000005ECBBF09; ABTEST=1|1593929499|v17; SUV=1593929499299150; browerV=3; osV=2; SNUID=2D33E261CACC60616B3C5025CAE899C9; taspeed=taspeedexist; sct=2; ld=Blllllllll2KlQDPlllllVDxtnylllll$hsfiZllllUlllllxylll5@@@@@@@@@@; sst0=660',
        'DNT: 1',
        'Host: www.sogou.com',
        'Sec-Fetch-Dest: document',
        'Sec-Fetch-Mode: navigate',
        'Sec-Fetch-Site: none',
        'Sec-Fetch-User: ?1',
        'Upgrade-Insecure-Requests: 1',
        'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
    );
    $html = getUrl($url, $header);
//    var_dump($html);
//    exit;
    for ($i = 0; $i < 15; $i++) {
        $data[] = get_tag_data($html, "cite", "id", "cacheresult_info_" . $i);
    }
//    var_dump($data);exit;
    return $data;
}

function get_360_data($url)
{
    $header = array(
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Encoding: gzip, deflate, br',
        'Accept-Language: zh-CN,zh;q=0.9',
        'Cache-Control: max-age=0',
        'Connection: keep-alive',
//        'Cookie: QiHooGUID=02D798457366C6C0FD77EAC309EA8023.1593843467889; __guid=15484592.644155273448980600.1593843467444.9766; webp=1; stc_ls_sohome=RQzX2jYRKi!uTRXMM(W); __huid=11yUi255qtZ694%2FXnyqECI8U1H5IoEtqfccgS0eA1noZE%3D; dpr=1; _S=s5rl77m4gdppvhva9sjr2ilms0; opqopq=ee4e3cda8009e19d247670a98a447b3c.1594004507; gtHuid=1; _uc_silent=1; count=5; erules=p1-10%7Cp2-10%7Cecr-1%7Cp3-4%7Cp4-15%7Cecl-1',
        'Host: www.so.com',
        'Sec-Fetch-Dest: document',
        'Sec-Fetch-Mode: navigate',
        'Sec-Fetch-Site: same-origin',
        'Sec-Fetch-User: ?1',
        'Upgrade-Insecure-Requests: 1',
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36',
    );
    while (1) {
        $html = getUrl($url, $header);
        if ($html != false) {
            break;
        }
    }
    $content = str_get_html($html);
//    var_dump($html);exit;
    foreach ($content->find('p[class=res-linkinfo] cite') as $element) {
        $data[] = $element->innertext;
    }
//    var_dump($data);exit;
    return $data;
}

function get_baidum_data($url)
{
    $header = array(
        'Accept: */*',
        'Accept-Encoding: gzip, deflate, br',
        'Accept-Language: zh-CN,zh;q=0.9',
        'Cache-Control: max-age=0',
        'Connection: keep-alive',
//    'Cookie: tt_webid=6847285490573477383; csrftoken=9fe119d0a573674e9a5d637499088df7; W2atIF=1; _ga=GA1.2.1075029636.1594257904; _gid=GA1.2.1078831002.1594257904; __tasessionId=32umi57o31594257910438; WIN_WH=414_736; PIXIEL_RATIO=3; SLARDAR_WEB_ID=6847285490573477383',
//    'Referer: https://m.toutiao.com/search?keyword=%E6%B2%B3%E5%8D%97%E7%9C%81%E5%8D%97%E9%98%B3%E5%B8%82%E7%A4%BE%E5%8C%BA%E5%9B%A2%E8%B4%AD&pd=synthesis&source=input&traffic_source=&original_source=&in_tfs=&in_ogs=',
        'Host: m.baidu.com',
        'Sec-Fetch-Dest: document',
        'Sec-Fetch-Mode: navigate',
        'Sec-Fetch-Site: none',
        'Sec-Fetch-User: ?1',
        'Upgrade-Insecure-Requests: 1',
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36',
    );

    while (1) {
        $html = getUrl($url, $header);
        $data = get_tag_data($html, "span", "class", "c-showurl c-footer-showurl");
        if ($html != false && strstr($html, '百度安全验证') == false) {
//            if(!empty($data)){
            break;
//            }
        }
    }
//    var_dump($html);exit;
    return $data;
}

//curl get
function getUrl($url, $header = false)
{
    $ch = curl_init($url);
//    $data = [
//        'tps172.kdlapi.com',
//        'tps129.kdlapi.com'
//    ];
//    $i = mt_rand(0, 1);

    curl_setopt($ch, CURLOPT_PROXY, 'tps129.kdlapi.com');
    curl_setopt($ch, CURLOPT_PROXYPORT, "15818");
//    curl_setopt($ch, CURLOPT_PROXYUSERPWD, "代理用户:代理密码");

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回数据不直接输出
    curl_setopt($ch, CURLOPT_ENCODING, "gzip"); //指定gzip压缩

    //add header
    if (!empty($header)) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    //add ssl support
    if (substr($url, 0, 5) == 'https') {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    }
    //add 302 support
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    // curl_setopt($ch, CURLOPT_COOKIEFILE, $this->lastCookieFile); //使用提交后得到的cookie数据
    $content = '';
    try {
        $content = curl_exec($ch); //执行并存储结果
    } catch (Exception $e) {
//         var_dump($e->getMessage());
    }
//    $curlError = curl_error($ch);

    curl_close($ch);
    return $content;
}

/**
 * @param $keyword //关键字
 * @param $keyword_url //排名网址
 * @param int $type //搜索引擎类型 1百度 2搜狗 3神马搜索 4.360 5百度移动端
 * @param int $list_id //原列表id
 * @return array
 */
function getRank($keyword, $keyword_url, $list_id, $type = '1')
{
    $result = array();
    $rank = 0; //网址排名
    $db = DB::getIntance();

    switch ($type) {
        case 1:
            $page = 0; //百度分页

            for ($i = 0; $i < 1; $i++) {
//                $keyword = '云南省文山壮族苗族自治州终影骑士';
                $url = "http://www.baidu.com/s?wd=" . $keyword . "&pn=" . $page . '&rn=50&tn=json';
                while (true) {
                    $html = curl_get_baidu($url);
                    $data = json_decode($html, true);
//                    var_dump($html);exit;
                    if (!empty($data)) {
                        break;
                    }
//                    if (substr($html, 0, 4) != '<a h' && $html != false && $html !== '') {//人机验证
//                        break;
//                    }
                }
//                echo json_encode($data['feed']['entry']);
//                exit;
                if (is_array($data['feed']['entry'])) {
                    foreach ($data['feed']['entry'] as $value) {
                        if (isset($value['url'])) {
                            $result[] = $value['url'];
                        }
                    }
                }
                $page += 10;
            }
            if (!empty($result)) {
                foreach ($result as $key => $value) {
                    if (strstr($value, $keyword_url) != false) {
                        $rank = $key + 1;
                        break;
                    }
                }
            }
            $source = 'baidu';
            break;
        case 2://搜狗
            for ($i = 1; $i < 4; $i++) {
//                $keyword = '四川省泸州市新风系统';
                $url = "https://www.sogou.com/web?query=" . urlencode($keyword) . "&page=" . $i;
                $res = get_sogou_data($url);
//                var_dump($res);
//                exit;
                foreach ($res as $value) {
                    if (!empty($value)) {
                        $result[] = $value[0];
                    }
                }
            }
            foreach ($result as $key => $value) {
                if (strstr($value, $keyword_url) != false) {
                    $rank = $key + 1;
                    break;
                }
            }
            $source = 'sogou';
            break;
        case 3://神马
            $url = "https://m.sm.cn/s?q=" . $keyword . "&from=smor&safe=1&by=submit&snum=0";
            $html = get_sm_data($url);
            $data = str_get_html($html);
            if (!empty($data)) {
                foreach ($data->find('a[class=c-flex-1 c-line-clamp-1 c-e-source]') as $value) {
                    $result[] = $value->href;
                }
            }
            if (!empty($result)) {
                foreach ($result as $key => $value) {
                    if (strstr($value, $keyword_url) != false) {
                        $rank = $key + 1;
                        break;
                    }
                }
            }
//            var_dump($rank);exit;
            $source = 'sm';
            break;
        case 4://360
            for ($i = 1; $i < 4; $i++) {
//                $keyword = '二手中央空调回收哪家好';
                $url = "https://www.so.com/s?q=" . $keyword . "&pn=" . $i;
                $res = get_360_data($url);
//                var_dump($res);
//                exit;
                foreach ($res as $value) {
                    if (!empty($value)) {
                        $result[] = $value;
                    }
                }
            }
//            var_dump($result);
//            exit;
            foreach ($result as $key => $value) {
                if (strstr($value, $keyword_url) != false) {
                    $rank = $key + 1;
                    break;
                }
            }
            $source = '360';
            break;

        case 5: //baidum
            $page = 0;
            for ($i = 1; $i < 4; $i++) {
                $url = 'https://m.baidu.com/s?wd=' . $keyword . '&pn=' . $page;
                $res = get_baidum_data($url);

                foreach ($res as $value) {
                    if (!empty($value)) {
                        $result[] = $value;
                    }
                }
            }
            foreach ($result as $key => $value) {
                if (strstr($value, $keyword_url) != false) {
                    $rank = $key + 1;
                    break;
                }
            }
            $source = 'baidum';
            break;
        default:
    }

    $db->insert('log', array(
        'content' => json_encode($result),
        'source' => $source,
        'list_id' => $list_id,
        'res_count' => count($result),
        'update_time' => date('Y-m-d H:i:s', time()),
    ));
    return $rank;
}

//function a()
//{
//百度直接抓网页
//    for ($i = 0; $i < 1; $i++) {
//        $url = "http://www.baidu.com/s?wd=" . $keyword . "&pn=" . $page . '&rn=50';
//        while (true) {
//            $html = curl_get_baidu($url);
//            if (substr($html, 0, 4) != '<a h') {//人机验证
//                break;
//            }
//        }
//        if (strstr($html, '很抱歉，没有找到与') != false) {
//            break;
//        }
//        $data1 = get_tag_data($html, "a", "class", "c-showurl"); //百度自己的链接
//        $data2 = get_tag_data($html, "a", "class", "c-showurl "); //三方地址
//        if (!empty($data1) && empty($data2)) {
//            break;
//        }
//        foreach ($data2 as $value) {
//            array_push($result, $value);
//        }
//        $page += 10;
//    }
//    if (!empty($result)) {
//        foreach ($result as $key => $value) {
//            if (strstr($value, $keyword_url) != false) {
//                $rank = $key + 1;
//            }
//        }
//    }

//百度api
//    $page = 0; //百度分页
//
//    for ($i = 0; $i < 1; $i++) {
//        $keyword = '广东省韶关市抖音云控招商';
//        $url = "http://www.baidu.com/s?wd=" . $keyword . "&pn=" . $page . '&rn=50&tn=json';
//        while (true) {
//            $html = curl_get_baidu($url);
//            if (substr($html, 0, 4) != '<a h' && $html != false) {//人机验证
//                break;
//            }
//        }
//        $data = json_decode($html, true);
//        var_dump($html);
//        exit;
//        foreach ($data['feed']['entry'] as $value) {
//            if (isset($value['url'])) {
//                array_push($result, $value['url']);
//            }
//        }
//        $page += 10;
//    }
//    $db = DB::getIntance();
//    $db->insert('log', array(
//        'content' => json_encode($result),
//        'source' => 'baidu',
//    ));
//    if (!empty($result)) {
//        foreach ($result as $key => $value) {
//            if (strstr($value, $keyword_url) != false) {
//                $rank = $key + 1;
//            }
//        }
//    }
//}

/**
 * 获取指定日期段内每一天的日期
 * @param $startdate 开始日期
 * @param $enddate 结束日期
 * @return Array
 */
function getDateFromRange($startdate,$enddate)
{
    if (!$enddate) {
        $enddate = date('Y-m-d', time());
    }
    $stimestamp = strtotime($startdate);
    $etimestamp = strtotime($enddate);
    if ($etimestamp > time()) {
        return array();
    }
    // 计算日期段内有多少天
    $days = ($etimestamp - $stimestamp) / 86400 + 1;

    // 保存每天日期
    $date = array();

    for ($i = 0; $i < $days; $i++) {
        $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
    }

    return $date;
}

//点击量
function click_nums($type)
{
    $cur_h = date('H', time());
    $data = 0;
    switch ($type) {
        case 1: //点击量小于150
            $data = 1 + (150 / 24) + (24 - $cur_h);
            break;
        case 2: // 点击量大于150小于300
            $data = 1 + (24 - $cur_h);
            break;
    }

    return $data;
}

/**
 * @param $cpid 企业id
 * @param $date 日期
 */
function insert_visit($cpid, $date)
{
    $db = DB::getIntance();
    $db_table = 'days_visits';

    $sql = "select id,visit from " . $db_table . " where `cpid` = '" . $cpid . "' and `date` = '" . $date . "'";
    $res = $db->getRow($sql);

    if (empty($res)) {
        $db->insert($db_table, array(
            'cpid' => $cpid,
            'date' => $date,
            'visit' => mt_rand(20, 150),
            'created_at' => date('Y-m-d H:i:s', time())
        ));
    } else {
        if ($res['visit'] < 20) {
            $db->update($db_table,
                array('visit' => mt_rand(20, 150), 'updated_at' => date('Y-m-d H:i:s', time())),
                array('id' => $res['id']));
        }
    }
}

/**
 * 检查指定字符串是否为日期格式 年-月-日
 * @param $date  日期字符串
 * @return bool  true 是日期格式     false 不是日期格式
 */
function valid_date($date)
{
    //匹配日期格式
    if (preg_match("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts)) {
        //检测是否为日期,checkdate为月日年
        if (checkdate($parts[2], $parts[3], $parts[1]))
            return true;
        else
            return false;
    } else
        return false;
}

//curl post方式并去除header
function curl_post($url, $headers, $bodys)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, true);
    if (1 == strpos("$" . $url, "https://")) {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }
    curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
    $result = curl_exec($curl);
//    $bodyInfo = '';
    if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {
        $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); //获取header长度
        $bodyInfo = substr($result, $headerSize); //截取掉header
    }
    curl_close($curl);
    return $bodyInfo;
}

function get_res($db_table1, $db_table2)
{
    $res = DB::table($db_table1)
        ->leftJoin($db_table2, function ($join) use ($db_table1, $db_table2) {
            $join->on($db_table1 . '.name', '=', $db_table2 . '.keyword')
                ->where($db_table2 . '.p_id', '=', 0);
        })
        ->select($db_table1 . '.id', $db_table1 . '.name', $db_table2 . '.p_id')
        ->where($db_table2 . '.p_id', '=', null)
        ->orderBy($db_table1 . '.id')
//        ->limit('5000')
        ->get();
    return $res;
}